package me.olook.netease.song.rank.biz.impl;

import me.olook.netease.song.rank.biz.WeiboUserService;
import me.olook.netease.song.rank.dto.FollowersCount;
import me.olook.netease.song.rank.entity.WeiboUser;
import me.olook.netease.song.rank.mapper.WeiboUserJpaDao;
import me.olook.netease.song.rank.util.DateTimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Optional;

/**
 * @author zhaohw
 * @date 2018-05-24 9:51
 */
@Service
public class WeiboUserServiceImpl implements WeiboUserService {

    @Autowired
    private WeiboUserJpaDao weiboUserJpaDao;

    @Override
    public void save(WeiboUser weiboUser) {
        weiboUserJpaDao.save(weiboUser);
    }

    @Override
    public Optional<WeiboUser> findById(String uid) {
        return weiboUserJpaDao.findById(uid);
    }

    @Override
    public Page<WeiboUser> findByPage(Pageable pageable) {
        return weiboUserJpaDao.findAll(pageable);
    }

    @Override
    public Page<WeiboUser> findFansTopUsers(String name, int page ,int size) {
        Sort sort = new Sort(Sort.Direction.DESC, "followersCount");
        return weiboUserJpaDao.findWeiboUserByAddBy(name, PageRequest.of(page,size,sort));
    }

    @Override
    public List<String> findDistinctByAddBy() {
        return weiboUserJpaDao.findAllAddBy();
    }

    @Override
    public  List<Object[]> countAddByAndGender(String addBy) {
        return weiboUserJpaDao.countAddByAndGender(addBy);
    }

    @Override
    public List<FollowersCount> countFollowersSegment(String addBy) {
        List<FollowersCount> result = new ArrayList<>();
        Long count1 = weiboUserJpaDao.countWeiboUsersByFollowersCountBetweenAndAddByEquals(0,1000,addBy);
        Long count2 = weiboUserJpaDao.countWeiboUsersByFollowersCountBetweenAndAddByEquals(1000,3000,addBy);
        Long count3 = weiboUserJpaDao.countWeiboUsersByFollowersCountBetweenAndAddByEquals(3000,10000,addBy);
        Long count4 = weiboUserJpaDao.countWeiboUsersByFollowersCountBetweenAndAddByEquals(10000,100000,addBy);
        Long count5 = weiboUserJpaDao.countWeiboUsersByFollowersCountBetweenAndAddByEquals(100000,500000,addBy);
        Long count6 = weiboUserJpaDao.countWeiboUsersByFollowersCountBetweenAndAddByEquals(500000,999999999,addBy);
        result.add((new FollowersCount(1,count1.intValue(),"0-1K")));
        result.add((new FollowersCount(2,count2.intValue(),"1K-3K")));
        result.add((new FollowersCount(3,count3.intValue(),"3K-1W")));
        result.add((new FollowersCount(4,count4.intValue(),"1W-10W")));
        result.add((new FollowersCount(5,count5.intValue(),"10W-50W")));
        result.add((new FollowersCount(6,count6.intValue(),"50W+")));
        return result;
    }

    @Override
    public List<WeiboUser> findAllByCrtTimeAfter(String name) {
        List<WeiboUser> recentData = new ArrayList<>(10);
        List<WeiboUser> twoDaysData = weiboUserJpaDao.findAllByCrtTimeAfterAndAddByEquals(DateTimeUtil.getLastDayStart(),name);
        twoDaysData.stream().peek(p->{
            String createdAt = p.getCreatedAt();

            //Web搜索数据无此字段
            if(StringUtils.isEmpty(createdAt)){

            }else{
                if(p.getCreatedAt().contains("刚刚")){

                }else if(p.getCreatedAt().contains("分钟")){
                    Integer minute = Integer.parseInt(p.getCreatedAt().substring(0,p.getCreatedAt().indexOf("分钟")));
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(p.getCrtTime());
                    calendar.add(Calendar.MINUTE, -minute);
                    p.setCrtTime(calendar.getTime());
                }else if(p.getCreatedAt().contains("小时")){
                    Integer hour = Integer.parseInt(p.getCreatedAt().substring(0,p.getCreatedAt().indexOf("小时")));
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(p.getCrtTime());
                    calendar.add(Calendar.HOUR, -hour);
                    p.setCrtTime(calendar.getTime());
                }else{
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(p.getCrtTime());
                    calendar.add(Calendar.MONTH, -1);
                    p.setCrtTime(calendar.getTime());
                }
            }
        }).sorted((e1,e2)->{
            return e2.getCrtTime().compareTo(e1.getCrtTime());
        }).limit(10)
                .forEach(recentData::add);

        return recentData;
    }

    @Override
    public Long countAll() {
        return weiboUserJpaDao.count();
    }

}
